<?php
/**
* Infotex UK Showimg - script to open a JPG file and resize dynamically
*
* script to resize a JPG image
*
* @package     showimg
* @author     John Harman Infotex UK Ltd
* @license    Commercial
* @copyright  2005 Infotex UK Ltd www.infotex.co.uk
* @version    1.1
*/

//per-site variables - directory and final sizes
	$dirname="controlpanel/shoppics/";
	$lgeheight=200;
	$smlheight=100;
	$noimgsmall="//images//noimg.jpg";
	$noimglarge="//images//noimg.jpg";

//=============================

   if (preg_replace("/[^0-9]/","",$_GET["id"])!=="") {  
// find DB include file
		if (file_exists("inc_dbdetails.php")) {
			include ("inc_dbdetails.php");
		} else {
			if (file_exists("includes/inc_dbdetails.php")) {
				include ("includes/inc_dbdetails.php");
			} else {
			//format=user, password, dbname, server
				$dbdetails=array("test", "test", "test", "localhost");
			};
		};
		$db=mysql_connect($dbdetails[3],$dbdetails[0],$dbdetails[1]);
		@mysql_select_db($dbdetails[2],$db) or die( "ERROR: Unable to open database, please check details");
		$strsel="SELECT image FROM products WHERE id=\"".preg_replace("/[^0-9]/","",$_GET["id"])."\";";
		$rsresult=mysql_query($strsel,$db);
		$num=mysql_num_rows($rsresult);
	
		if ($num>0) {
			$imgname=mysql_result($rsresult,0,"image");
		};
		mysql_close();
   };
//having found image name, now prepend directory & check that it exists

	if ($imgname=='') {notfound();}
	$file_name = $dirname.$imgname;
	if (file_exists($file_name)==FALSE) {
		notfound();
	}

	$jpgfile=ImageCreateFromJPEG($file_name);
	header("Content-type: image/jpeg");

	if ($_GET['size']=='lge') {
		$resultheight=$lgeheight;
	} else {
		$resultheight=$smlheight;
	};

//now find out perspective of image and create new image bank to output
	$aspectratio=$resultheight/imagesy($jpgfile);
	$resultwidth=intval(imagesx($jpgfile)*$aspectratio);
	$outputjpgfile=imagecreatetruecolor($resultwidth, $resultheight);
	imagecopyresampled($outputjpgfile,$jpgfile,0,0,0,0,$resultwidth,$resultheight,imagesx($jpgfile),imagesy($jpgfile));
	imagejpeg($outputjpgfile,'',60);

//having output it now tidy up after us

	imagedestroy($jpgfile);
	imagedestroy($outputjpgfile);
	exit;
/**
 #########################
 * function to return a default image is needed
 * @return string
 #########################
*/
function notfound() {
	global $noimgsmall, $noimglarge;
	if ($_GET['size']=="lge") {
		$jpgfile=ImageCreateFromJPEG(getcwd().$noimglarge);
	} else {
		$jpgfile=ImageCreateFromJPEG(getcwd().$noimgsmall);
	};
	header("Content-type: image/jpeg");
	imagejpeg($jpgfile);
	imagedestroy($jpgfile);
	exit;
};
?>